
import disLine10kv from "../../geojson/10kVLine.json"

const RiverPlayer = function (viewer, Cesium) {
  var self = this
  this.viewer = viewer
  this.Cesium = Cesium
  this.Cts3Arrays = []
  //断面中心点cts3
  this.sectionCts3Arrays = []
  this.startSection = null
  this.TmpCts3Arrays = []
  this.currentCts3Arrays = []
  this.RoadMap = new Map();
}
RiverPlayer.prototype = {
  init() {
    disLine10kv.features.forEach(featur => {
      this.RoadMap.set(featur.properties.Name, featur.geometry.coordinates)
    })

  },
  flowPlay(name) {
    let coordinates = this.RoadMap.get(name)
    if (coordinates) {
      try {
        let self = this
        let latlonArray = this.arrayFlat(coordinates)
        self.Cts3Arrays = this._getPowerLinePosition(latlonArray, false)

        if (!this.polylineEntity) {

          this.polylineEntity = new this.Cesium.Entity({
            id: "riverID",
            polyline: {
              positions: //Cts3Arrays,
                new this.Cesium.CallbackProperty(function () {
                  return self.currentCts3Arrays
                }, false),
              clampToGround: true,
              width: 6
              // classificationType: this.Cesium.ClassificationType.BOTH
            }
          })
          this.viewer.entities.add(this.polylineEntity)
        }

      
        // let sectionlatlonArray = this.arrayFlat(sectionPoint)
        // self.sectionCts3Arrays = sectionPoint.concat()
        // self.startSection = self.sectionCts3Arrays.shift()

        this.play()
        // this.viewer.flyTo(polylineEntity)
      } catch (err) {

      }
    }

  },
  play() {
    let self = this
    //清除工作要做好，清除之前播放的
    this.TmpCts3Arrays = this.getInsetPoints(this.Cts3Arrays, 5)
    if (self.playInterval) {
      window.clearInterval(self.playInterval)
    }
    self.playInterval = setInterval(function () {
      let newpoi = self.TmpCts3Arrays.shift()
      if (newpoi) {
        var heading = self.Cesium.Math.toRadians(0.0);
        var pitch = self.Cesium.Math.toRadians(-60.0);
        var range = 500.0;
        //视角跟随
        self.viewer.camera.lookAt(newpoi, new self.Cesium.HeadingPitchRange(heading, pitch, range));
        //慢慢延长河流线
        debugger
        self.currentCts3Arrays.push(newpoi)
      } else {
        self.viewer.camera.lookAtTransform(self.Cesium.Matrix4.IDENTITY);
        window.clearInterval(self.playInterval)
      }
    }, 100)
  },
  endPlay() {
    if (this.playInterval) {
      window.clearInterval(this.playInterval)
    }
    this.currentCts3Arrays = []
  },
  //插值获取平滑路线
  getInsetPoints(points, minDis) {
    var result = []
    //判断两点的距离
    for (var i = 0; i < points.length - 1; i++) {
      var disCoumt = this.Cesium.Cartesian3.distance(points[i], points[i + 1])
      let count = disCoumt / minDis
      if (count > 0) {
        for (var j = 0; j < count; ++j) {
          var offset = j / count;
          let insdtPoi = this.Cesium.Cartesian3.lerp(
            points[i],
            points[i + 1],
            offset,
            new this.Cesium.Cartesian3()
          );
          result.push(insdtPoi)
        }
      } else {
        result.push(points[i + 1])
      }

    }

    return result
  },
  _getPowerLinePosition(positions, isHeight) {
    try {
      if (isHeight) {
        return this.Cesium.Cartesian3.fromDegreesArrayHeights(positions)
      } else {
        return this.Cesium.Cartesian3.fromDegreesArray(positions)
      }
    } catch (e) {
      return [];
    }

  },
  /**
   * 二维数组变一维数组
   * @param positions
   * @returns {*}
   */
  arrayFlat(positions) {
    try {
      let pos = positions.flat();
      return pos;
    } catch (e) {

    }
  },
  guid() {
    function S4() {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }

    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
  }

}

export default RiverPlayer